package com.extension.company.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.extension.company.admin.R;

/**
 * 作者：zwk
 * 时间：on 2019/3/27 20:06
 * 功能：购物数量的自定义控件
 */
public class CartNumCalcultor extends LinearLayout implements View.OnClickListener {

	/**
	 * 默认最小值0
	 */
	private static final int DEFUALT_MIN = 0;
	/**
	 * 默认最大值是1000
	 */
	private static final int DEFUALT_MAX = 1000;

	private ImageView cut, add;
	private TextView num;

	private int value;
	private int minValue;
	private int maxValue;

	private OnNumCalculatorClickListener mOnNumCalculatorClickListener;

	public CartNumCalcultor(Context context) {
		this(context, null);
	}

	public CartNumCalcultor(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CartNumCalcultor(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context);

		if (attrs != null) {
			TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.cartNumCalcultorStyleable);

			int value = typedArray.getInt(R.styleable.cartNumCalcultorStyleable_value, 0); // 获取xml中设置的默认的值
			int maxValue = typedArray.getInt(R.styleable.cartNumCalcultorStyleable_maxValue, 0);  //获取设置的最大的值
			int minValue = typedArray.getInt(R.styleable.cartNumCalcultorStyleable_minValue, 0);  //获取设置的最小的值
			Drawable numBackground = typedArray.getDrawable(R.styleable.cartNumCalcultorStyleable_editBackground);// 获取设置的数量的背景颜色
			Drawable addBackground = typedArray.getDrawable(R.styleable.cartNumCalcultorStyleable_buttonAddBackground); // +的背景颜色
			Drawable cutBackground = typedArray.getDrawable(R.styleable.cartNumCalcultorStyleable_buttonSubBackground);// -的背景颜色


			setValue(value);
			setMinValue(minValue);
			setMaxValue(maxValue);
			if (null != numBackground) {
				setNumTextViewBackground(numBackground);
			}
			if (null != addBackground) {
				setAddBackground(addBackground);
			}
			if (null != cutBackground) {
				setCutBackground(cutBackground);
			}


			typedArray.recycle();
		}
	}


	private void initView(Context context) {

		View view = LayoutInflater.from(context).inflate(R.layout.cart_calcultor, this, true);
		cut = view.findViewById(R.id.car_cut);
		add = view.findViewById(R.id.car_add);
		num = view.findViewById(R.id.car_num);

		cut.setOnClickListener(this);
		add.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.car_cut:
				subNum();
				if (mOnNumCalculatorClickListener != null) {
					mOnNumCalculatorClickListener.onCutClick(v, this.value);
				}
				break;
			case R.id.car_add:
				addNum();
				if (mOnNumCalculatorClickListener != null) {
					mOnNumCalculatorClickListener.onAddClick(v, this.value);
				}
				break;
		}

	}

	private void subNum() {
		getValue();

		if (this.value > minValue) {
			this.value = --this.value;
		}
		num.setText(this.value+"");

	}

	private void addNum() {
		getValue();
		if (this.value < maxValue) {
			this.value = ++this.value;
		}
		num.setText(this.value+"");
	}

	public void setValue(int value) {
		num.setText(value + "");
		this.value = value;
	}

	public int getValue() {
		String value = num.getText().toString().trim();
		if (null != value && !"".equals(value)) {
			this.value = Integer.parseInt(value);
		}
		return this.value;

	}

	public void setMaxValue(int maxValue) {
		this.maxValue = maxValue;
	}

	public void setMinValue(int minValue) {
		this.minValue = minValue;
	}

	public void setNumTextViewBackground(Drawable drawable) {
		num.setBackground(drawable);
	}

	public void setNumTextViewBackground(int drawableId) {
		setNumTextViewBackground(getResources().getDrawable(drawableId));
	}

	public void setAddBackground(Drawable drawable) {
		add.setBackground(drawable);
	}

	public void setAddBackground(int drawableId) {
		add.setBackground(getResources().getDrawable(drawableId));
	}

	public void setCutBackground(Drawable drawable) {
		add.setBackground(drawable);
	}

	public void setCutBackground(int drawableId) {
		add.setBackground(getResources().getDrawable(drawableId));
	}

	public void setOnNumCalculatorClickListener(OnNumCalculatorClickListener onNumCalculatorClickListener) {
		this.mOnNumCalculatorClickListener = onNumCalculatorClickListener;

	}


	public interface OnNumCalculatorClickListener {
		void onAddClick(View view, int value);

		void onCutClick(View view, int value);
	}
}
